using System;
using System.ComponentModel;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.Runtime;
using DP.Sharepoint.Workflow.InfoPath;
using Microsoft.SharePoint.WorkflowActions;

namespace DP.Sharepoint.Workflow
{
    public partial class SetInfoPathFormValueInnerXml : Activity
    {
        public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context",
                                                                                         typeof (WorkflowContext),
                                                                                         typeof (
                                                                                             SetInfoPathFormValueInnerXml
                                                                                             ));

        public static DependencyProperty PropertyPathProperty = DependencyProperty.Register("PropertyPath",
                                                                                            typeof (string),
                                                                                            typeof (
                                                                                                SetInfoPathFormValueInnerXml
                                                                                                ));

        public static DependencyProperty PropertyValueProperty = DependencyProperty.Register("PropertyValue",
                                                                                             typeof (string),
                                                                                             typeof (
                                                                                                 SetInfoPathFormValueInnerXml
                                                                                                 ));

        private IPAccessHelper _ipHelper;

        public SetInfoPathFormValueInnerXml()
        {
            InitializeComponent();
        }

        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string PropertyPath
        {
            get { return ((string) (base.GetValue(PropertyPathProperty))); }
            set { base.SetValue(PropertyPathProperty, value); }
        }

        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string PropertyValue
        {
            get { return ((string) (base.GetValue(PropertyValueProperty))); }
            set { base.SetValue(PropertyValueProperty, value); }
        }


        [Description("Context")]
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public WorkflowContext __Context
        {
            get { return ((WorkflowContext) (base.GetValue(__ContextProperty))); }
            set { base.SetValue(__ContextProperty, value); }
        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            _ipHelper = new IPAccessHelper(__Context);


            try
            {
                _ipHelper.LoadForm();
            }
            catch (Exception e)
            {
                Exception we = Common.WrapWithFriedlyException(e,
                                                               "Error loading form data file. Likely causes: this file is not a valid InfoPath Form!");

                Common.LogExceptionToWorkflowHistory(we, executionContext, WorkflowInstanceId);

                throw we;
            }

            FormSetFieldValueRequest myRequest = new FormSetFieldValueRequest();

            myRequest.IPAccessHelper = _ipHelper;

            myRequest.PropertyPath = PropertyPath;

            myRequest.PropertyValue = PropertyValue;

            myRequest.SetValueType = FormSetValueType.InnerXml;

            WorkflowEnvironment.WorkBatch.Add(IPAccessService.Instance, myRequest);


            return ActivityExecutionStatus.Closed;
        }
    }
}